package com.zuikaku.dao;

import com.zuikaku.pojo.Order;
import com.zuikaku.pojo.User;
import com.zuikaku.tool.JDBCPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {


    public static List<Order> getOrderByUserId(int userId){
        Connection connection = JDBCPool.getConnection();
        String selectSql = "SELECT * FROM t_order WHERE fk_user_id = ?";
        List<Order> orderList = new ArrayList<>();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(selectSql);
            preparedStatement.setInt(1, userId);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                Order order =new Order();
                order.setOrderId(rs.getInt("order_id"));
                order.setOrderName(rs.getString("order_name"));
                order.setOrderStatus(rs.getInt("order_status"));
                order.setOrderPrice(rs.getInt("order_price"));
                order.setFkUserId((rs.getInt("fk_user_id")));
                order.setCreateDate(rs.getTimestamp("create_date"));
                orderList.add(order);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return orderList;

    }

}
